package com.example.cyw.mycontextprover;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by cyw on 2017/12/5.
 */

public class StuAdapter extends BaseAdapter {
    private Context mcontext;
    private ArrayList<student> mlist;
    private DelListen del;
    public  StuAdapter(Context mcontext,ArrayList<student> mlist,DelListen del){
        this.mcontext=mcontext;
        this.mlist=mlist;
        this.del=del;
    }
    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mlist.get(position).getId();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHold hold;
        if (convertView==null){
            hold=new ViewHold();
            convertView= LayoutInflater.from(mcontext).inflate(R.layout.stu_item,parent,false);
            hold.id=(TextView) convertView.findViewById(R.id.id);
            hold.name=(TextView) convertView.findViewById(R.id.name);
             hold.age=(TextView) convertView.findViewById(R.id.age);
            hold.del=(Button) convertView.findViewById(R.id.btn_del);
            convertView.setTag(hold);
        }
        hold= (ViewHold) convertView.getTag();
        int id=mlist.get(position).getId();
        hold.id.setText(String.valueOf(id));
        hold.name.setText(mlist.get(position).getName());
        int age=mlist.get(position).getAge();
        hold.age.setText(String.valueOf(age));
        hold.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id=mlist.get(position).getId();
               del.del(id);

            }
        });


        return convertView;
    }
    class ViewHold{
        TextView id;
        TextView name;
        TextView age;
        Button del;
    }
}
